<template>
  <el-container class="home-container">
    <!-- 1. 顶部 -->
    <el-header>
      <div class="left">
        <img src="@/assets/logo.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 2.主体部分 -->
    <el-container class="main">
      <!-- 2.1 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 
          background-color="#333744" 背景色
text-color="#fff" 字体颜色 active-text-color="#409EFF" 被激活的状态颜色
unique-opened  是否值开启一个 :collapse="isCollapse" 左右的折叠
 :collapse-transition="false" 是否开启动画 router 开启路由模式
       -->
        <div class="collpase" @click="isCollapse = !isCollapse">| | |</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened
          :collapse="isCollapse" :collapse-transition="false" router>
          <el-submenu :index="item.id + ''" v-for="item in menus" :key="item.id">
            <!-- 一级路由 -->
            <template slot="title">
              <i :class="iconsObj[item.id + '']" style="margin-right:10px"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级路由 -->
            <!-- {{ item.children }}  -->
            <el-menu-item :index="'/' + it.path" v-for="it in item.children" :key="it.id">
              <i class="el-icon-menu"></i>
              <span slot="title">{{ it.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <!-- 2.2主体部分 -->
      <el-main>
        <!-- 二级路由出口 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import { getMenus } from '@/api/home.js'
export default {
  name: "home-com",
  data() {
    return {
      isCollapse: false,
      menus: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
    }
  },
  created() {
    this.getMunes()
  },
  methods: {
    async getMunes() {
      // 1、发送请求
      let { data: { data, meta } } = await getMenus()
      //  2.如果失败则 提示
      if (meta.status != 200) return this.$message.error("菜单请求失败!")
      this.menus = data
    },
    async  logout(){
        let res = await this.$confirm( '确定退出登录吗?', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(err=>err)
            // 点击的是取消  则 不再往下执行
          if(res=='cancel') return ;
        // 刪除token
        window.localStorage.removeItem("login-token")
        this.$router.push("/login")
      }
    },    
}
</script>
<style lang="scss">
.home-container {
  height: 100vh;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;

  img {
    height: 60px;
    border-radius: 50%;
    vertical-align: middle;
  }

  span {
    font-size: 18px;
    color: #eee;
    padding-left: 10px;
  }
}

.el-aside {
  background-color: #333744;
  overflow: hidden;
}

.el-main {
  background-color: #eaedf1;
}

.collpase {
  background-color: #4a5064;
  color: #fff;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3px;
  cursor: pointer;
}</style>
  